/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cis;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.JTable;

//this class manages the data in 3 product name combo boxes
public class CisProductNameComboBoxModel 
        extends AbstractListModel implements ComboBoxModel {

    //the product table in Products view
    JTable productTable;
    //the selected item
    Object selectedItem;
    
    //constructor
    CisProductNameComboBoxModel(JTable productTable) {
        this.productTable = productTable;
        setSelectedItem("<Select a Product>");
    }

    //get the number of items in the combo box
    public int getSize() {
        return productTable.getModel().getRowCount() + 1;
    }

    //get the item at index
    public Object getElementAt(int index) {
        if (index == 0)
           return "<Select a Product>";
        else
            return productTable.getModel().getValueAt(index - 1, 1);
    }

    //set the selected item
    public void setSelectedItem(Object anItem) {
        selectedItem = anItem;
        fireContentsChangedEvent();
    }

    //get the selected item
    public Object getSelectedItem() {
        return selectedItem;
    }

    //fire events when items are changed
    public void fireContentsChangedEvent() {
        fireContentsChanged(this, 0, getSize() - 1);
    }
    
    //fire events when items are added
    public void fireIntervalAddedEvent() {
        fireIntervalAdded(this, 0, getSize() - 1);
    }
    
    //fire events when items are removed
    public void fireIntervalRemovedEvent() {
        setSelectedItem(getElementAt(0));
    }
}
